package com.yedean.mall.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.dto.UmsMenuNode;
import com.yedean.mall.entity.UmsMenu;
import com.yedean.mall.service.IUmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UmsMenuControoler
 *
 * @author yedean
 * @date 2022/8/3
 */
@RestController
@Api(tags = "后台菜单管理")
@RequestMapping("/admin/menu")
public class UmsMenuController {
    @Autowired
    private IUmsMenuService menuService;

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = menuService.getById(id);
        return CommonResult.success(umsMenu);
    }
    
    @ApiOperation("树形结构返回所有菜单列表")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    public CommonResult<List<UmsMenuNode>> treeList(){
        List<UmsMenuNode> umsMenuNodes = menuService.treeList();
        return CommonResult.success(umsMenuNodes);
    }

    @ApiOperation("添加后台菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<?> create(@RequestBody UmsMenu umsMenu) {
        boolean save = menuService.save(umsMenu);
        return save?CommonResult.success():CommonResult.failed();
    }


    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult<?> update(@PathVariable Long id,
                               @RequestBody UmsMenu umsMenu) {

        UpdateWrapper<UmsMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean update = menuService.update(umsMenu, wrapper);
        return update?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation("分页查询后台菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "页大小"),
            @ApiImplicitParam(name = "pageNum",value = "当前页")
    })
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsMenu>> list(@PathVariable Long parentId,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        //获取分页数据
        IPage<UmsMenu> pageList = menuService.pageList(parentId, pageSize, pageNum);
        
        return CommonResult.success(CommonPage.resetPage(pageList));
    }


    @ApiOperation("修改菜单显示状态")
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    public CommonResult<?> updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        //更新隐藏状态
       menuService.updateHidden(id,hidden);
       
       
       return CommonResult.success();
    }
    
    
    
}
